using System;
using System.Collections.Generic;
using System.Linq;
using WalkingTec.Mvvm.Core;
using System.ComponentModel.DataAnnotations;
using HengTong.Model.Db.Hr.Bill;
using HengTong.Model.Extension;
using TiaoTaoHelper.Wtm.Models;
using TiaoTaoHelper.Wtm.Interfaces;
using WalkingTec.Mvvm.Core.Extensions;


namespace HengTong.ViewModel.Hr.SysHrRecruitPublishRequestBillVms
{
    public partial class
        SysHrRecruitPublishRequestBillListVm : BaseListVm<SysHrRecruitPublishRequestBillView,
        SysHrRecruitPublishRequestBillSearcher>
    {
        protected override IEnumerable<IGridColumn<SysHrRecruitPublishRequestBillView>> InitGridHeader()
        {
            return new List<GridColumn<SysHrRecruitPublishRequestBillView>>
            {
                this.MakeGridHeader(x => x.Title),
                this.MakeGridHeader(x => x.Description),
                this.MakeGridHeader(x => x.HireNumber),
                this.MakeGridHeader(x => x.ExpectedDate),
                this.MakeGridHeader(x => x.JobRequirement),
                this.MakeGridHeader(x => x.EducationRequirement),
                this.MakeGridHeader(x => x.MajorRequirement),
                this.MakeGridHeader(x => x.GenderRequirement),
                this.MakeGridHeader(x => x.AgeRequirement),
                this.MakeGridHeader(x => x.OtherRequirement),
                this.MakeGridHeader(x => x.SalaryRange),
                this.MakeGridHeader(x => x.Benefits),
                this.MakeGridHeader(x => x.JobPositionName),
                this.MakeGridHeader(x => x.PublishStartDate),
                this.MakeGridHeader(x => x.PublishEndDate),
                this.MakeGridHeader(x => x.IsExternalPublish),
                this.MakeGridHeader(x => x.ContactPerson),
                this.MakeGridHeader(x => x.ContactPhone),
                this.MakeGridHeader(x => x.ContactEmail),
                this.MakeGridHeader(x => x.Approver),
                this.MakeGridHeader(x => x.ApprovalTime),
                this.MakeGridHeader(x => x.GroupName),
                this.MakeGridHeader(x => x.OrgName),
                this.MakeGridHeader(x => x.BillCode),
                this.MakeGridHeader(x => x.BillStatus),
                this.MakeGridHeader(x => x.WorkflowStatus),
                this.MakeGridHeader(x => x.Remark),
                this.MakeGridHeader(x => x.CanApprove).SetHide().SetFormat((a, b) =>
                {
                    if (a.CanApprove)
                    {
                        return "true";
                    }
                    else
                    {
                        return "false";
                    }
                }),

                this.MakeGridHeaderAction(width: 200)
            };
        }

        public override IOrderedQueryable<SysHrRecruitPublishRequestBillView> GetSearchQuery()
        {
            var query = DC.Set<SysHrRecruitPublishRequestBill>()
                .CheckContain(Searcher.Title, x => x.Title)
                .CheckContain(Searcher.Description, x => x.Description)
                .CheckEqual(Searcher.HireNumber, x => x.HireNumber)
                .CheckBetween(Searcher.ExpectedDate?.GetStartTime(), Searcher.ExpectedDate?.GetEndTime(),
                    x => x.ExpectedDate, includeMax: false)
                .CheckContain(Searcher.JobRequirement, x => x.JobRequirement)
                .CheckEqual(Searcher.EducationRequirement, x => x.EducationRequirement)
                .CheckContain(Searcher.MajorRequirement, x => x.MajorRequirement)
                .CheckEqual(Searcher.GenderRequirement, x => x.GenderRequirement)
                .CheckContain(Searcher.AgeRequirement, x => x.AgeRequirement)
                .CheckContain(Searcher.OtherRequirement, x => x.OtherRequirement)
                .CheckContain(Searcher.SalaryRange, x => x.SalaryRange)
                .CheckContain(Searcher.Benefits, x => x.Benefits)
                .CheckEqual(Searcher.JobPositionId, x => x.JobPositionId)
                .CheckBetween(Searcher.PublishStartDate?.GetStartTime(), Searcher.PublishStartDate?.GetEndTime(),
                    x => x.PublishStartDate, includeMax: false)
                .CheckBetween(Searcher.PublishEndDate?.GetStartTime(), Searcher.PublishEndDate?.GetEndTime(),
                    x => x.PublishEndDate, includeMax: false)
                .CheckEqual(Searcher.IsExternalPublish, x => x.IsExternalPublish)
                .CheckContain(Searcher.ContactPerson, x => x.ContactPerson)
                .CheckContain(Searcher.ContactPhone, x => x.ContactPhone)
                .CheckContain(Searcher.ContactEmail, x => x.ContactEmail)
                .CheckContain(Searcher.Approver, x => x.Approver)
                .CheckBetween(Searcher.ApprovalTime?.GetStartTime(), Searcher.ApprovalTime?.GetEndTime(),
                    x => x.ApprovalTime, includeMax: false)
                .CheckEqual(Searcher.GroupId, x => x.GroupId)
                .CheckEqual(Searcher.OrgId, x => x.OrgId)
                .CheckContain(Searcher.BillCode, x => x.BillCode)
                .CheckEqual(Searcher.BillStatus, x => x.BillStatus)
                .CheckEqual(Searcher.WorkflowStatus, x => x.WorkflowStatus)
                .CheckContain(Searcher.Remark, x => x.Remark)
                .Select(x => new SysHrRecruitPublishRequestBillView
                {
                    ID = x.ID,
                    Title = x.Title,
                    Description = x.Description,
                    HireNumber = x.HireNumber,
                    ExpectedDate = x.ExpectedDate,
                    JobRequirement = x.JobRequirement,
                    EducationRequirement = x.EducationRequirement,
                    MajorRequirement = x.MajorRequirement,
                    GenderRequirement = x.GenderRequirement,
                    AgeRequirement = x.AgeRequirement,
                    OtherRequirement = x.OtherRequirement,
                    SalaryRange = x.SalaryRange,
                    Benefits = x.Benefits,
                    JobPositionName = x.JobPosition.Name,
                    PublishStartDate = x.PublishStartDate,
                    PublishEndDate = x.PublishEndDate,
                    IsExternalPublish = x.IsExternalPublish,
                    ContactPerson = x.ContactPerson,
                    ContactPhone = x.ContactPhone,
                    ContactEmail = x.ContactEmail,
                    Approver = x.Approver,
                    ApprovalTime = x.ApprovalTime,
                    GroupName = x.Group.GroupName,
                    OrgName = x.Org.Name,
                    BillCode = x.BillCode,
                    BillStatus = x.BillStatus,
                    WorkflowStatus = x.WorkflowStatus,
                    Remark = x.Remark,
                    CreateTime = x.CreateTime // 添加CreateTime属性
                })
                .OrderByDescending(x => x.CreateTime);
            return query;
        }

        public override void AfterDoSearcher()
        {
            if (Wtm.LoginUserInfo != null && typeof(IBill).IsAssignableFrom(ModelType))
            {
                List<SysWorkflow> ids = GetMyApproves();
                if (ids.Any())
                {
                    foreach (var item in EntityList)
                    {
                        var m = ids.Find(x => x.ModelId == item.GetID().ToString());
                        if (m != null && item.WorkflowStatus == WorkflowStatus.审核中)
                        {
                            item.CanApprove = true;
                        }
                    }
                }
            }


            EntityList.ForEach(e =>
            {
                e.CreateBy = Wtm.FormatUsername(e.CreateBy);
                e.UpdateBy = Wtm.FormatUsername(e.UpdateBy);
                e.Approver = Wtm.FormatUsername(e.Approver);
            });

            base.AfterDoSearcher();
        }
    }

    public class SysHrRecruitPublishRequestBillView : SysHrRecruitPublishRequestBill
    {
        [Display(Name = "岗位名称")] public String JobPositionName { get; set; }
        [Display(Name = "_Admin.GroupName")] public String GroupName { get; set; }
        [Display(Name = "组织名称")] public String OrgName { get; set; }
        public bool CanApprove { get; set; }
    }
}